<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class My_Model  extends CI_Model {
    
    protected $pagesize=20;
    protected $tablename;
    protected $adminid=0;
    protected $messages;
    protected $model_name;
    public $table_prefix="dream_";

    public function __construct()
    {
        parent::__construct();
        if(property_exists(get_instance(),"ion_auth")){
            $this->adminid=get_instance()->ion_auth->get_user_id();
        }
        $this->load->database();
        $this->load->library('pagination');
        $this->messages    = array();        
    }

    public function get_item_by_where($where)
    {
        $query=$this->db->where($where)->get($this->table_prefix.$this->tablename);
        $item=$query->row_array();
        if($item){
            return $item;
        }else{
            return false;
        }
    }

    public function get_name_by_where($where)
    {
        $query=$this->db->where($where)->select('name')->get($this->table_prefix.$this->tablename);
        $item=$query->row_array();
        if($item){
            return $item;
        }else{
            return false;
        }
    }

    protected function delete($where)
    {
        $this->db->where($where);
        $this->db->delete($this->table_prefix.$this->tablename);
        return true;
    }

    protected function auto_add($key,$where,$val=1)
    {
        $this->db->set($key,$key.' + '.$val,false);
        $this->db->where($where);
        $this->db->update($this->table_prefix.$this->tablename);
    }


    protected function getcount($where)
    {
        $this->db->select('count(*) AS num');
        if(!is_array($where) && strstr($where,"REGEXP"))
        {
            $this->db->where($where,"",false);
        }
        else
        {
            $this->db->where($where);
        }
        //$this->db->from($this->table_prefix.$this->tablename);
        $query = $this->db->get($this->table_prefix.$this->tablename); 
        if($query->num_rows() > 0){  
            $count_result = $query->row_array();  
            $count = $count_result['num'];  
        }else{  
            return 0;
        } 
        //$total = $this->db->count_all_results();
        return $count;
    }


    protected function get_items_by_where_page($where,$page=1,$size=100)
    {
        $offset=$size*($page-1);
        $query=$this->db->where($where)->limit($size, $offset)->order_by('id asc')->get($this->table_prefix.$this->tablename);
        $items=$query->result_array();
        if($items){
            return $items;
        }else{
            return false;
        }
    }

    public function max($key,$where)
    {
        if($where)
        {
            $this->db->where($where);
        }
        $this->db->select_max($key,"max");
        $query = $this->db->get($this->table_prefix.$this->tablename);
        $item=$query->row_array();
        if($item)
        {
            return $item["max"];
        }
        else
        {
            return 0;
        }
    }

    protected function get_items_by_where($where,$order="id desc",$feild="*")
    {
        if(!is_array($where) && strstr($where,"REGEXP"))
        {
            $query=$this->db->select($feild)->where($where,"",false)->order_by($order)->get($this->table_prefix.$this->tablename);
        }
        else
        {
            $query=$this->db->select($feild)->where($where)->order_by($order)->get($this->table_prefix.$this->tablename);
        }
        
        $items=$query->result_array();
        if($items){
            return $items;
        }else{
            return false;
        }
    }

    protected function get_items_by_page($where,$page,$base_url,$order="id desc",$join=array(),$key='',$cols="*",$totalcount=0)
    {
        if($where)
        {
            if(!is_array($where) && strstr($where,"REGEXP"))
            {
                $this->db->where($where,"",false);
            }
            else
            {
                $this->db->where($where);
            }
        }        
        if(is_array($key) && $key)
        {
            $icount=0;
            $this->db->group_start();
            foreach($key as $kk=>$vv)
            {
                if($icount>0)
                {
                    $this->db->or_like($kk, $vv);
                }
                else
                {
                    $this->db->like($kk, $vv);
                }
                $icount++;
            }
            $this->db->group_end();
        }
        else if($key)
        {
            $this->db->like('name', $key);
        }
        $this->db->from($this->table_prefix.$this->tablename);
        if($totalcount>0)
        {
            $total=$totalcount;
            $this->db->reset_query();
        }
        else
        {
            $total=$this->db->count_all_results();
        }

        $offset=$this->pagesize*($page-1);        
        if(!is_array($where) && strstr($where,"REGEXP"))
        {
            $this->db->where($where,"",false);
        }
        else
        {
            $this->db->where($where);
        }
        if($join)
        {
            if(isset($join["pattern"]))
            {
                $this->db->join($this->table_prefix.$join["table"],$join["where"],$join["pattern"]);
            }
            else
            {
                $this->db->join($this->table_prefix.$join["table"],$join["where"]);
            }
            
        }
        if(is_array($key) && $key)
        {
            $icount=0;
            $this->db->group_start();
            foreach($key as $kk=>$vv)
            {
                if($icount>0)
                {
                    $this->db->or_like($kk, $vv);
                }
                else
                {
                    $this->db->like($kk, $vv);
                }
                $icount++;
            }
            $this->db->group_end();
        }
        else if($key)
        {
            $this->db->like('name', $key);
        }
        $this->db->select($cols);
        $query=$this->db->from($this->table_prefix.$this->tablename)->limit($this->pagesize, $offset)->order_by($order)->get();
        $data=$query->result_array();        
        $config['base_url']=site_url($base_url);
        $config['total_rows']=$total;
        $config['per_page']=$this->pagesize;        
        $this->pagination->initialize($config);
        $pager=$this->pagination->create_links();
        $pages=ceil($total/$this->pagesize);  
        return array('data'=>$data,'pager'=>$pager,'total'=>$total,'pages'=>$pages);
    }

    protected function add_item($data)
    {
        $this->db->insert($this->table_prefix.$this->tablename, $data);
        $id = $this->db->insert_id();
        if($id){
            $data=$this->get_item_by_where(array("id"=>$id));
        }
        $this->set_message($this->model_name."添加成功");
        return (isset($id)) ? $data : FALSE;        
    }

    protected function update_item($where,$data){
        if($this->db->update($this->table_prefix.$this->tablename,$data,$where)){
            $this->set_message("编辑".$this->model_name."成功");
            return true;
        }else{
            $this->set_message("编辑".$this->model_name."失败");
            return false;
        }
    }


    public function set_message($message)
    {
        $this->messages[] = $message;
    
        return $message;
    }
    
    public function messages(){
        $_output = '';
        foreach ($this->messages as $message)
        {
            $_output .= '##' . $message . '##';
        }
        return $_output;
    }

}